package cn.endeavour.org.service.impl;

import cn.endeavour.basic.exception.BusinessException;
import cn.endeavour.basic.service.impl.BaseServiceImpl;
import cn.endeavour.org.domain.Shop;
import cn.endeavour.org.mapper.EmployeeMapper;
import cn.endeavour.org.mapper.ShopMapper;
import cn.endeavour.org.service.IEmployeeService;
import cn.endeavour.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IEmployeeService employeeService;

    @Override
    public void update(Shop shop) {
        super.update(shop);

    }

    @Override
    @Transactional
    public void settlement(Shop shop) {
        // 先判断是否以及入驻过
        if (shopMapper.isSettled(shop) != null) {
            throw new BusinessException("店铺已经入驻过,请直接登录,忘记密码请找回");
        }
        // 在添加员工前先查询,员工是否存在
        if (employeeMapper.findByAccount(shop.getAdmin()) != null) {
            // 说明存在有这个员工
            Long accountId = employeeMapper.findAccountId(shop.getAdmin());
            // 给shop中的id进行赋值
            shop.getAdmin().setId(accountId);
        } else {
            // 然后添加员工表
            employeeService.add(shop.getAdmin());
        }
        shopMapper.save(shop);
        // 然后添加员工id
        employeeMapper.addShop(shop);
//        employeeMapper.updateShopId(shop);
    }
}
